//
//  LFPredicateTool.m
//  AntMarket
//
//  Created by LF on 2017/3/20.
//  Copyright © 2017年 LF. All rights reserved.
//

#import "LFPredicateTool.h"

@implementation LFPredicateTool

/**
 *  邮箱正则判断
 *
 *  @param email
 *
 *  @return YES/NO
 */
+ (BOOL) validateEmainl:(NSString *)email
{
    NSString * emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    
    NSPredicate * emailText = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",emailRegex];
    
    return [emailText evaluateWithObject:email];
}


/**
 *  手机号验证
 *
 *  @param mobile 手机号
 *
 *  @return YES/NO
 */
+ (BOOL) validateMobile:(NSString *)mobile
{
    //    NSString * mobileRegex = @"^((13[0-9])|(15[^4,\\D])|(17[0-9])|(18[0,0-9]))\\d{8}$";
    NSString * mobileRegex = @"^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\\d{8}$";
    
    NSPredicate * mobileTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",mobileRegex];
    
    return [mobileTest evaluateWithObject:mobile];
}

/**
 *  手机号验证
 *
 *  @param phone 手机号
 *
 *  @return YES/NO
 */
+ (BOOL) validatePhone:(NSString *)phone
{
    NSString * phoneRegex = @"1[3|4|5|7|8][0-9]{9}]";
    
    NSPredicate * phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    
    return [phoneTest evaluateWithObject:phone];
}

/**
 *  用户名验证
 *
 *  @param name 用户名
 *
 *  @return YES/NO
 */
+ (BOOL) validateUserName:(NSString *)name
{
    NSString * userNameRegex = @"^[A-Za-z0-9]{6,20}+$";
    
    NSPredicate * userNamePreicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex];
    
    BOOL judge = [userNamePreicate evaluateWithObject:name];
    
    return judge;
}

/**
 *  密码正则
 *
 *  @param password 密码
 *
 *  @return YES/NO
 */
+ (BOOL) validatePassword:(NSString *)password
{
    NSString * passwordRegex = @"^[a-zA-Z0-9]{6,20}+$";
    
    NSPredicate * passwordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passwordRegex];
    
    return [passwordPredicate evaluateWithObject:password];
}

/**
 *  昵称正则
 *
 *  @param nickname 昵称
 *
 *  @return YES/NO
 */
+ (BOOL) validateNickname:(NSString *)nickname
{
    NSString * nicknameRegex = @"^[\u4e00-\u9fa5]{4,8}$";
    
    NSPredicate * nicknamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nicknameRegex];
    
    return [nicknamePredicate evaluateWithObject:nickname];
}

/**
 *  身份证正则
 *
 *  @param identityCard 身份证号码
 *
 *  @return YES/NO
 */
+ (BOOL) validateIdentityCard:(NSString *)identityCard
{
    BOOL flag;
    
    if (identityCard.length <= 0)
    {
        flag = NO;
        
        return flag;
    }
    
    NSString * regex = @"^(\\d{14}\\d{17})(\\d|[xX])$";
    
    NSPredicate * identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    
    return [identityCardPredicate evaluateWithObject:identityCard];
}


/**
 *  验证码去除空格
 *
 *  @param code 验证码
 *
 *  @return realCode
 */
+ (NSString *)checkCode:(NSString *)code {
    NSArray *array = [code componentsSeparatedByString:@" "];
    NSString *realCode = @"";
    for (int i = 0; i < array.count; i++) {
        realCode = [NSString stringWithFormat:@"%@%@",realCode,array[i]];
    }
    return realCode;
}

@end
